<?php
/**
 * 登录,登出,验证
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: AuthController.php 1082 2011-12-09 09:27:48Z maomao.thx@gmail.com $
 */
class AuthController extends IG_Controller_Action
{
    public function noAuthAction()
    {
        $this->setRender();
        if ($this->getRequest()->isPost()) {
            $this->_helper->message('很抱歉，您没有权限查看该页！');
        } else {
            echo '很抱歉，您没有权限查看该页！';
        }
    }

    public function loginAction()
    {
        $this->disableLayout();
        $this->view->headTitle()->append('用户登录');
        if ($this->getRequest()->isPost()) {
            $params = $this->_getParams(array('username', 'password',
                                              'verifycode', 'passpod'));

            $auth = Zend_Auth::getInstance();
            IG_Auth_Adapter_Iwgame::setEnableVerify(false);
            $authAdapter = new IG_Auth_Adapter_Iwgame($params['username'],
                                                      $params['password'],
                                                      $params['verifycode'],
                                                      $params['passpod']);
            $result = $auth->authenticate($authAdapter);

            if ($result->isValid()) {
                $this->_helper->json(array('statusCode' => 0));
            } else {
                $data = array('statusCode' => abs($result->getCode()-1),
                              'message' => $result->getMessages());
                $data['verifycode'] = $this->_refreshValidate();
                $this->_helper->json($data);
            }
        } else if ($this->_getParam('verifycode') == 'refresh') {
            $this->_helper->json(array('statusCode' => 0, 'verifycode' => $this->_refreshValidate()));
        } else {
            $this->view->assign('enableCryptoguard', IG_Cryptoguard::isEnabled());
            $this->view->assign("verifycode", $this->_refreshValidate());
        }
    }

    protected function _refreshValidate()
    {
        $captcha = IG_Auth_Adapter_Iwgame::createVerifyCode();
        return $captcha->getImgUrl() . $captcha->getId() . $captcha->getSuffix();
    }

    /**
     *
     * 退出登录
     */
    public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect('/auth/login');
    }
}